if ( WScript.Arguments.Length == 0 )
{
	WScript.Echo( "Usage: <script> <textfile>" );
	WScript.Quit();
}

var fso = WScript.CreateObject( "Scripting.FileSystemObject" );
var file = fso.OpenTextFile( WScript.Arguments( 0 ) );
var players = {};
while ( !file.AtEndOfStream )
{
	var fields = file.ReadLine().split( /\s*,\s*/i );
	var player = fields[ 0 ];
	var pos = fields[ 1 ];
	var games = fields[ 2 ];
	var starts = fields[ 3 ];

	if ( !players.hasOwnProperty( player ) )
		players[ player ] = { name:player, positions:new Array(), pitcher:false };

	switch ( pos )
	{
	case 'P':
		players[ player ].pitcher = true;
		if ( starts >= 3 )
			players[ player ].positions.push( 'SP' );
		if ( games - starts >= 5 )
			players[ player ].positions.push( 'RP' );
		break;

	default:
		players[ player ].pitcher = false;
		if ( starts >= 5 || games >= 10 )
			players[ player ].positions.push( pos );
		break;
	}
}

var sortedPlayers = new Array();
for ( var player in players )
	if ( players[ player ].positions.length > 0 )
		sortedPlayers.push( { name:players[ player ].name, positions:players[ player ].positions, pitcher:players[ player ].pitcher } );

WScript.Echo( 'By name:' );
sortedPlayers.sort( byName );
for ( var i = 0; i < sortedPlayers.length; i++ )
	WScript.Echo( sortedPlayers[ i ].name + ', ' + sortedPlayers[ i ].positions.join( ',' ) );

WScript.Echo( '\r\nBy number of qualifying positions:' );
sortedPlayers.sort( byNumPos );
for ( var i = 0; i < sortedPlayers.length; i++ )
	WScript.Echo( sortedPlayers[ i ].name + ', ' + sortedPlayers[ i ].positions.join( ',' ) );

function byName( a, b )
{
	if ( a.name < b.name )
		return -1;
	if ( a.name > b.name )
		return 1;
	return 0;
}

function byNumPos( a, b )
{
	if ( a.pitcher && !b.pitcher )
		return -1;
	if ( b.pitcher && !a.pitcher )
		return 1;

	if ( a.positions.length > b.positions.length )
		return -1;
	if ( a.positions.length < b.positions.length )
		return 1;
	return 0;
}

//WScript.Echo( player + ' ' + players[ player ].join( ',' ) );
